﻿using System.Collections.Generic;
using System.Linq;

namespace Common
{
    /// <summary>
    /// Name - UserManualSectionCollection.cs
    /// Description - 
    /// Programmer - CSC440 Team One
    /// Date coded - 11/27/2011
    /// Arguments - IBusinessService
    /// Variables - None
    /// Files accessed by this code artifact - None
    /// Files changed by this code artifact - None
    /// I/O - None
    /// Error Handling - None
    /// Modifications -
    ///     1.0 - Initial coding
    /// Known Faults -
    ///     1.0 - None known
    /// </summary>
    public class UserManualSectionCollection
    {
        private UserManualSection _Root;

        private Dictionary<int, UserManualSection> children;

        /// <summary>
        /// Gets the root.
        /// </summary>
        public UserManualSection Root
        {
            get
            {
                return _Root;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserManualSectionCollection"/> class.
        /// </summary>
        public UserManualSectionCollection()
        {
            _Root = new UserManualSection()
            {
                Id = 0,
                Title = "Root"
            };

            children = new Dictionary<int, UserManualSection>();

            children.Add(0, _Root);
        }

        public UserManualSectionCollection(IEnumerable<UserManualSection> allUserManualSections)
        {
            _Root = new UserManualSection()
            {
                Id = 0,
                Title = "Root"
            };

            children = new Dictionary<int, UserManualSection>();

            children.Add(0, _Root);

            if (allUserManualSections.Any())
            {
                foreach (var userManulSection in allUserManualSections)
                {
                    children.Add(userManulSection.Id, userManulSection);
                }

                foreach (var userManualSection in allUserManualSections)
                {
                    if (children.ContainsKey(userManualSection.Parent))
                    {
                        children[userManualSection.Parent].AddChild(userManualSection);
                    }
                }
            }
        }

        /// <summary>
        /// Adds the specified user manual section.
        /// </summary>
        /// <param name="userManualSection">The user manual section.</param>
        public void Add(UserManualSection userManualSection)
        {
            var key = userManualSection.Id;

            if (!children.ContainsKey(key))
            {
                children.Add(key, userManualSection);
            }

            key = userManualSection.Parent;

            if (children.ContainsKey(key))
            {
                children[key].AddChild(userManualSection);
            }
        }

        /// <summary>
        /// Adds the new user manual section.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public UserManualSection AddNewUserManualSection(int parent)
        {
            var key = -1;

            while (children.ContainsKey(key))
            {
                key--;
            }

            var newUserManualSection = new UserManualSection()
            {
                Id = key,
                Parent = parent
            };

            children.Add(key, newUserManualSection);

            if (children.ContainsKey(parent))
            {
                children[parent].AddChild(newUserManualSection);
            }

            return newUserManualSection;
        }

        /// <summary>
        /// Finds the user manual section by id.
        /// </summary>
        /// <param name="sectionId">The section id.</param>
        /// <returns></returns>
        public UserManualSection FindUserManualSectionById(int sectionId)
        {
            if (children.ContainsKey(sectionId))
            {
                return children[sectionId];
            }
            else
            {
                return null;
            }
        }
    }
}
